package msgo

import (
	"reflect"
	"strings"
	"unicode"
	"unsafe"
)

func SubStringLast(str string, subStr string) string {
	// 先查有没有
	index := strings.Index(str, subStr)
	if index == -1 {
		return ""
	}
	length := len(subStr)
	return str[index+length:]
}

func isASCII(s string) bool {
	for i := 0; i < len(s); i++ {
		if s[i] > unicode.MaxASCII {
			return false
		}
	}

	return true
}

func BytesToString(byte []byte) string {
	return *(*string)(unsafe.Pointer(&byte))
}

func StringToBytes(s string) []byte {
	return unsafe.Slice(unsafe.StringData(s), len(s))
}

func StringToBytesV2(s string) []byte {
	return *(*[]byte)(unsafe.Pointer(
		&struct {
			string
			Cap int
		}{s, len(s)},
	))
}
func StringToBytesV3(s string) []byte {
	sh := (*reflect.SliceHeader)(unsafe.Pointer(&s))
	bh := reflect.SliceHeader{
		Data: sh.Data,
		Len:  sh.Len,
		Cap:  sh.Cap,
	}
	return *(*[]byte)(unsafe.Pointer(&bh))
}
